<?php

class Exam_TestController extends Core_Controller_Action_Standard {
	
    public function init(){
        if(!$this->_helper->requireUser->isValid()) return;
        
    }
    
    public function testAction(){
        
        $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
            ->getNavigation('exam_main', array(), 'exam_main_test');
       
        $this->view->subnavigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
            ->getNavigation('exam_main_tests', array(), 'exam_main_test_test');
        $type = $this->view->type = $this->_getParam('mt',1);
        $this->view->numberquestion = 0;
        if(!$this->_helper->requireUser()->isValid()) return;
        $exam_id = $this->_getParam('id',1);
        $viewer = Engine_Api::_()->user()->getViewer();
        $testSession = new Zend_Session_Namespace('Exam_Excute_Test');
        
        $this->view->exam_id = $exam_id;
        $exam = Engine_Api::_()->getItem('exam',$exam_id);
        $this->view->exam = $exam;
        // check valid exam;
        if(!$exam) {
            $this->view->error = true;
            $this->view->message = '';
            return;
        }
        //if( !$this->_helper->requireAuth()->setAuthParams('exam', null, 'test')->isValid() ) return;
        if(empty($exam->approve)){
            $this->view->error = true;
            $this->view->message = '';
            return;
        }
        if(!empty($exam->starttime)&& $exam->starttime != '0000-00-00 00:00:00'){
            $starttime = strtotime($exam->starttime);
            $endtime = strtotime($exam->endtime);
            $currenttime = time();
            if($starttime < $currenttime){
                $this->view->error = true;
                $this->view->message = '';
                return;
            }
            if($currenttime > $endtime){
                $this->view->error = true;
                $this->view->message = '';
                return;
            }
        }
        if(!$exam->isAllow($viewer->getIdentity())){
            $this->view->error = true;
            $this->view->message = '';
            return ;
        }
        
        if(!$this->getRequest()->isPost()){
            $testSession->start_time = time();
            return;
        }
        
        $userstarttime = $testSession->start_time;
        $currentime = time();
        $duration = $currentime - $userstarttime;
        if(!empty($exam->duration) ){
            if($duration > $exam->duration){
                $this->view->error = true;
                $this->view->message = '';
                return;
            }
            
        }
        
        $answers = array();
        $values = $this->_getAllParams();
        
        foreach($values as $key =>$answer){
            $type = explode('_',$key);
            if(is_array($type)){
                if($type[0] == 'radio'){
                    $answers[$type[1]]  = array($answer);
                }
                if($type[0] == 'checkbox'){
                    if(!is_array($answer)) $answer = array($answer);
                    $answers[$type[1]]  = $answer;
                }
            }
        }
        $this->view->useranswers = $answers;
        $testTable = Engine_Api::_()->getDbtable('tests','exam');
        $testTable->newTest($viewer->getIdentity(),$exam,$answers,array('starttime'=>$userstarttime,'endtime'=>$currentime));
        $this->view->finishTest = true;
        $this->view->exam = $exam;
        
        
        
    }
    
    public function historyAction(){
        if(!$this->_helper->requireUser->isValid()) return;
        $viewer = Engine_Api::_()->user()->getViewer();
        
        $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
            ->getNavigation('exam_main', array(), 'exam_main_test');
        $this->view->subnavigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
            ->getNavigation('exam_main_tests', array(), 'exam_main_test_history');
      
      
        $options = array();
        $category_id = $this->_getParam('category_id',0);
        $title = $this->_getParam('title','');
        $order = $this->_getParam('order','exam_id');
        $direction = $this->_getParam('direction','desc');
        $starttime = $this->_getParam('starttime','0000-00-00 00:00:00');
        $endtime = $this->_getParam('endtime',date('Y-m-d H:i:s'));
        $options['category_id'] = $category_id;
        $options ['title'] = $title;
        $options['order'] = $order;
        $options['direction'] = $direction;
        $options['starttime'] = $starttime;
        $options['endtime'] = $endtime;
        
        $eT = Engine_Api::_()->getDbTable('exams','exam');
        $sl = $eT->getSelectHistoryTest($viewer->getIdentity(),$options);
        
        $paginator = Zend_Paginator::factory($sl);
        $paginator->setItemCountPerPage(20);
        $paginator->setCurrentPageNumber($this->_getParam('page',0));
        $this->view->paginator = $paginator;
        $this->view->options = $options;
    }
    
    public function previewAction(){
        $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
            ->getNavigation('exam_main', array(), 'exam_main_test');
        $this->view->subnavigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
            ->getNavigation('exam_main_tests', array(), 'exam_main_test_test');
            
        $exam_id = $this->_getParam('exam_id',0);
        $exam = Engine_Api::_()->getItem('exam',$exam_id);
        $this->view->exam = $exam;
    }
    
    public function reviewAction(){
        if(!$this->_helper->requireUser->isValid()) return;
        $viewer = Engine_Api::_()->user()->getViewer();
        $exam_id = $this->_getParam('exam_id',0);
        if(empty($exam_id)){
           return  $this->_forward('success', 'utility', 'core', array(
              'smoothboxClose' => true,
              'parentRefresh' => false,
              'messages' => array(Zend_Registry::get('Zend_Translate')->_("Exam you select not found. Please select other exam."))
            ));
        }
        $order = $this->_getParam('order','test_id');
        $direction = $this->_getParam('direction','desc');
        
        $this->view->formValues = array('order'=>$order,'direction'=>$direction);
        
        $tT = Engine_Api::_()->getDbTable('tests','exam');
        $sl = $tT->getTestSelectBuyExam($exam_id,$viewer->getIdentity());
        $sl->order($order.' '.$direction);
        
        $paginator = Zend_Paginator::factory($sl);
        $paginator->setItemCountPerPage(20);
        $paginator->setCurrentPageNumber($this->_getParam('page',0));
        $this->view->paginator = $paginator;
    }
    
    public function correctAction(){
        $test_id = $this->_getParam('exam_id',0);
        if(empty($exam_id)){
           return $this->_forward('success', 'utility', 'core', array(
              'smoothboxClose' => true,
              'parentRefresh' => false,
              'messages' => array(Zend_Registry::get('Zend_Translate')->_("Test you select not found. Please select other test."))
            ));
        }
        $this->view->test_id = $test_id;
        
    }
    public function incorrectAction(){
        
    }
    public function allAction(){
        $test_id = $this->_getParam('exam_id',0);
        if(empty($test_id)){
            return $this->_forward('success', 'utility', 'core', array(
              'smoothboxClose' => true,
              'parentRefresh' => false,
              'messages' => array(Zend_Registry::get('Zend_Translate')->_("Test you select not found. Please select other test."))
            ));
        }
        $test = Engine_Api::_()->getItem('exam_test',$test_id);
        if(!$test){
            return $this->_forward('success', 'utility', 'core', array(
              'smoothboxClose' => true,
              'parentRefresh' => false,
              'messages' => array(Zend_Registry::get('Zend_Translate')->_("Test you select not found. Please select other test."))
            ));
        }
        $this->view->test = $test;
        $viewer = Engine_Api::_()->user()->getViewer();
        $this->view->user_id = $viewer->getIdentity();
        
    }
}
